package arabicScrabble;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class StartGameFrame extends JFrame implements ActionListener{

	 JButton startGameBtnE;
	 JButton startGameBtnA;
	JButton rulesBtn;
	JButton dictionaryBtn;
	JButton endGameBtn;
	JButton backBtn;
	JButton backBtnRules;


	JButton pplayer1;
	JButton pplayer2;
	JButton pplayer3;
	JButton pplayer4;

	JLabel rulesLbl;

	JPanel startGamePnl;
	JPanel noOfPlayersPnl;
	JPanel rulesPnl;

	int index;
	boolean arabic;

	public StartGameFrame(int index){

		this.index = index;
		arabic = false;
		this.setSize(400,100);
		setLocation(300,100);


		startGameBtnE = new JButton("Play English Scrabble");
		startGameBtnA = new JButton("Play Arabic Scrabble");
		rulesBtn = new JButton("Rules");
		dictionaryBtn = new JButton("Dictionary");
		endGameBtn = new JButton("End Game");
		backBtn = new JButton ("Back to Main Menu");
		backBtnRules = new JButton ("Back to Main Menu");

		startGamePnl = new JPanel();

		startGamePnl.add(startGameBtnE);
		startGamePnl.add(startGameBtnA);
		startGamePnl.add(rulesBtn);
		startGamePnl.add(dictionaryBtn);
		startGamePnl.add(endGameBtn);


		endGameBtn.addActionListener(this);
		startGameBtnE.addActionListener(this);
		startGameBtnA.addActionListener(this);

		pplayer1 = new JButton("1 Players");
		pplayer2 = new JButton("2 Players");
		pplayer3 = new JButton("3 Players");
		pplayer4 = new JButton("4 Players");

		pplayer1.addActionListener(this);
		pplayer2.addActionListener(this);
		pplayer3.addActionListener(this);
		pplayer4.addActionListener(this);




		rulesPnl = new JPanel();
		rulesLbl = new JLabel("<html>-Words may be formed by adding one or more letters to a word <br/>" +
				"  that is already on the board.<br/>" +
				"-By placing a word at right angles to a word already on the board. <br/>" +
				"-The new words must use one of the letters of the word already <br/>" +
				"  on the board.<br/> " +
				"-Letters can only be added to a word already on the board; in a<br/> " +
				"  straight line which follows the position the word was placed.<br/>" +
				"  (Vertically or Horizontally) <br/>" +
				"-No letter or word may be shifted or changed after it has been played. <br/>" +
				"-If a Players successfully uses all their 7 letters, they score an extra<br/>" +
				"  50 points.<br/>" +
				"-The system should automatically add 50 points to that Players's <br/>" +
				"  score in respect to the total word score that had just been formed.</html>");

		//	this.setContentPane(rulesPnl);
		rulesBtn.addActionListener(this);

			rulesPnl.add(rulesLbl);
			rulesPnl.add(backBtnRules);
			backBtnRules.setVisible(true);
			backBtnRules.addActionListener(this);
			backBtn.addActionListener(this);

		//	rulesPnl.setVisible(true);
		//	rulesPnl.repaint();




		noOfPlayersPnl = new JPanel();

		noOfPlayersPnl.add(pplayer1);
		noOfPlayersPnl.add(pplayer2);
		noOfPlayersPnl.add(pplayer3);
		noOfPlayersPnl.add(pplayer4);
		noOfPlayersPnl.add(backBtn);


		//noOfPlayersPnl.setVisible(false);

		JPanel currentPanel = null;

		if(index == 1){
			System.out.println("index 1");
			currentPanel = startGamePnl;

			this.setContentPane(currentPanel);
			this.setVisible(true);
		} else if (index == 2){
			System.out.println("index 2");
			currentPanel = noOfPlayersPnl;
			this.setContentPane(currentPanel);
			this.setVisible(true);
		}


		//mainFrame.pack();
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if(e.getActionCommand().equals("Play Arabic Scrabble")){
			this.setContentPane(noOfPlayersPnl);
			this.setVisible(true);
			noOfPlayersPnl.setVisible(true);
			System.out.println("start clicked");
			arabic = true;

			}
		if(e.getActionCommand().equals("Play English Scrabble")){
			this.setContentPane(noOfPlayersPnl);
			this.setVisible(true);
			noOfPlayersPnl.setVisible(true);
			System.out.println("start clicked");
			arabic=false;

			}
			else if(e.getActionCommand().equals("Rules")){
				System.out.println("rules clicked");
				this.setContentPane(rulesPnl);
				this.setVisible(true);
				this.setSize(WIDTH+475, HEIGHT+300);
				//rulesPnl.setVisible(true);

				rulesLbl.repaint();

			}
			else if(e.getActionCommand().equals("End Game")){
				System.exit(0);
			}

		if(e.getActionCommand().equals("Back to Main Menu")){
			this.setContentPane(startGamePnl);
			this.setVisible(true);
			startGamePnl.setVisible(true);
			System.out.println("back to main menu clicked");
			setSize(400, 100);

		}

			else if(e.getActionCommand().equals("2 Players")){
				MainApp m;
				try {
					m = new MainApp(2,arabic);

					System.out.println("Players 2");
					m.setVisible(true);
					this.setVisible(false);
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (UnsupportedEncodingException o) {
					// TODO Auto-generated catch block
					o.printStackTrace();
				}
			}
			else if(e.getActionCommand().equals("3 Players")){
				MainApp m;
				try {
					m = new MainApp(3,arabic);

					System.out.println("Players 3");
					m.setVisible(true);
					this.setVisible(false);

				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (UnsupportedEncodingException k) {
					// TODO Auto-generated catch block
					k.printStackTrace();
				}

			}
			else if(e.getActionCommand().equals("4 Players")){
				MainApp m;
				try {
					m = new MainApp(4,arabic);

					System.out.println("Players 4");
					m.setVisible(true);
					this.setVisible(false);

				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (UnsupportedEncodingException l) {
					// TODO Auto-generated catch block
					l.printStackTrace();
				}
			}else{
				if(e.getActionCommand().equals("1 Players")){
					MainApp m;
					try {
						m = new MainApp(1,arabic);

						System.out.println("Players 1");
						m.setVisible(true);
						this.setVisible(false);

					} catch (FileNotFoundException e1) {
						
						e1.printStackTrace();
					} catch (UnsupportedEncodingException h) {
						h.printStackTrace();
					}
			}

			}


}


public static void main(String[] args){
	StartGameFrame mainFrame = new StartGameFrame(1);
	mainFrame.getContentPane();
	mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

